package com.flyaudio.xiaopengautopilot.activity;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RemoteViews;

import com.flyaudio.xiaopengautopilot.R;
import com.flyaudio.xiaopengautopilot.util.DataInfo;
import com.flyaudio.xiaopengautopilot.constant.ModuleKey;

public class ControlWidgetProvider extends AppWidgetProvider {

    private int lfDoorLock, rfDoorLock, lrDoorLock, rrDoorLock = 0;
    /**
     * 每次窗口小部件被更新都调用一次该方法
     */
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_control_layout);
        remoteViews.setOnClickPendingIntent(R.id.widget_control_ll,  getOpenPendingIntent(context));
        setRemoteViews(context,remoteViews);

        for (int appWidgetId : appWidgetIds) {
            appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
        }
    }

    private void setRemoteViews(Context context, RemoteViews remoteViews) {
        if(DataInfo.getInstance().getDataParamerInfo(ModuleKey.BCM_DriverSideLock_Status) != null) {
            lfDoorLock = (int) DataInfo.getInstance().getDataParamerInfo(ModuleKey.BCM_DriverSideLock_Status);
        }
        if( DataInfo.getInstance().getDataParamerInfo(ModuleKey.BCM_PassengerSideLock_Status) != null) {
            rfDoorLock = (int) DataInfo.getInstance().getDataParamerInfo(ModuleKey.BCM_PassengerSideLock_Status);
        }
        if(DataInfo.getInstance().getDataParamerInfo(ModuleKey.BCM_LeftRearLock_Status) != null) {
            lrDoorLock = (int) DataInfo.getInstance().getDataParamerInfo(ModuleKey.BCM_LeftRearLock_Status);
        }
        if(DataInfo.getInstance().getDataParamerInfo(ModuleKey.BCM_RightRearLock_Status) != null) {
            rrDoorLock = (int) DataInfo.getInstance().getDataParamerInfo(ModuleKey.BCM_RightRearLock_Status);
        }
        if (lfDoorLock == 0 && rfDoorLock == 0 && lrDoorLock == 0 && rrDoorLock == 0) {
            String closeTx = context.getResources().getString(R.string.widget_control_carlock_close);
            String safeTx = context.getResources().getString(R.string.widget_control_vehicle_safe);
            remoteViews.setTextViewText(R.id.widget_control_carlock_status_tv,closeTx);
            remoteViews.setTextViewText(R.id.widget_control_vehicle_status_tv,safeTx);
            remoteViews.setImageViewResource(R.id.widget_control_carlock_iv, R.drawable.widget_control_carlock_close);
        } else {
            String openTx =context.getResources().getString(R.string.widget_control_carlock_open);
            String unSafeTx =  context.getResources().getString(R.string.widget_control_vehicle_unsafe);
            remoteViews.setTextViewText(R.id.widget_control_carlock_status_tv,openTx);
            remoteViews.setTextViewText(R.id.widget_control_vehicle_status_tv,unSafeTx);
            remoteViews.setImageViewResource(R.id.widget_control_carlock_iv, R.drawable.widget_control_carlock_open);
        }

    }

    /**
     * 获取 打开 MainActivity 的 PendingIntent
     */
    private PendingIntent getOpenPendingIntent(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, ControlActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        return pi;
    }

    /**
     * 接收窗口小部件点击时发送的广播
     */
    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        ComponentName componentName = new ComponentName(context, ControlWidgetProvider.class);
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_control_layout);
        setRemoteViews(context,remoteViews);
        AppWidgetManager awm = AppWidgetManager.getInstance(context);
        awm.updateAppWidget(componentName, remoteViews);
    }

    /**
     * 每删除一次窗口小部件就调用一次
     */
    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        super.onDeleted(context, appWidgetIds);
    }

    /**
     * 当最后一个该窗口小部件删除时调用该方法
     */
    @Override
    public void onDisabled(Context context) {
        super.onDisabled(context);
    }

    /**
     * 当该窗口小部件第一次添加到桌面时调用该方法
     */
    @Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
    }

    /**
     * 当小部件大小改变时
     */
    @Override
    public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {
        super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
    }

    /**
     * 当小部件从备份恢复时调用该方法
     */
    @Override
    public void onRestored(Context context, int[] oldWidgetIds, int[] newWidgetIds) {
        super.onRestored(context, oldWidgetIds, newWidgetIds);
    }
}
